package apis

import (
	"fmt"
	ccenginDto "vrcm/src/vrcm/app/admin/service/dto/ccengine"
	"vrcm/src/vrcm/common/apis"

	"github.com/gin-gonic/gin"
	"github.com/go-admin-team/go-admin-core/sdk/api"
	_ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"

	"vrcm/src/vrcm/app/admin/service/domian"
	"vrcm/src/vrcm/app/admin/service/dto/domian"
)

type Certificate struct {
	api.Api
}

// GetPage 获取Certificate列表
// @Summary 获取Certificate列表
// @Description 获取Certificate列表
// @Tags Certificate
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {object} response.Response{data=response.Page{list=[]models.Certificate}} "{"code": 200, "data": [...]}"
// @Router /api/v1/certificate [get]
// @Security Bearer
func (e Certificate) GetPage(c *gin.Context) {
	req := dto.CertificateGetPageReq{}
	s := service.Certificate{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	var count int64

	data, err := s.GetPage(&req, &count)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取证书列表失败， %s", err.Error()))
		return
	}

	e.PageOK(data, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

// Get 获取Certificate
// @Summary 获取Certificate
// @Description 获取Certificate
// @Tags Certificate
// @Param id path int false "id"
// @Success 200 {object} response.Response{data=models.Certificate} "{"code": 200, "data": [...]}"
// @Router /api/v1/certificate/{id} [get]
// @Security Bearer
func (e Certificate) Get(c *gin.Context) {
	req := dto.CertificateGetReq{}
	s := service.Certificate{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	data, err := s.Get(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取证书失败， %s", err.Error()))
		return
	}

	e.OK(data, "查询成功")
}

func (e Certificate) GetByCertId(c *gin.Context) {
	req := dto.CertificateGetByCertIdReq{}
	s := service.Certificate{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	data, err := s.GetByCertId(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取证书失败， %s", err.Error()))
		return
	}

	e.OK(data, "查询成功")
}

// Insert 创建Certificate
// @Summary 创建Certificate
// @Description 创建Certificate
// @Tags Certificate
// @Accept application/json
// @Product application/json
// @Param data body dto.CertificateInsertReq true "data"
// @Success 200 {object} response.Response	"{"code": 200, "message": "添加成功"}"
// @Router /api/v1/certificate [post]
// @Security Bearer
func (e Certificate) Insert(c *gin.Context) {
	req := dto.CertificateInsertReq{}
	s := service.Certificate{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.Insert(&req)

	if err != nil {
		if err.Error() == "证书校验失败" {
			e.Error(apis.CheckPemError.Code, err, fmt.Sprintf("创建证书校验失败，%s", err.Error()))
		} else if err.Error() == string(apis.CertificateNameError.Code) {
			e.Error(apis.CertificateNameError.Code, err, fmt.Sprintf("创建证书校验失败，%s", "证书名不能重复"))
		} else {
			e.Error(500, err, fmt.Sprintf("创建证书失败， %s", err.Error()))
		}
		return
	}

	e.OK(req.CertId, "创建成功")
}

// Update 修改Certificate
// @Summary 修改Certificate
// @Description 修改Certificate
// @Tags Certificate
// @Accept application/json
// @Product application/json
// @Param id path int true "id"
// @Param data body dto.CertificateUpdateReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "修改成功"}"
// @Router /api/v1/certificate/{id} [put]
// @Security Bearer
func (e Certificate) Update(c *gin.Context) {
	req := dto.CertificateUpdateReq{}
	s := service.Certificate{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	err = s.Update(&req)
	if err != nil {
		if err.Error() == "证书校验失败" {
			e.Error(apis.CheckPemError.Code, err, fmt.Sprintf("修改证书校验失败，%s", err.Error()))
		} else if err.Error() == string(apis.CertificateNameError.Code) {
			e.Error(apis.CertificateNameError.Code, err, fmt.Sprintf("修改证书校验失败，%s", "证书名不能重复"))
		} else if err.Error() == string(apis.DomainExistError.Code) {
			e.Error(apis.DomainExistError.Code, err, fmt.Sprintf("修改证书校验失败，%s", "域名不存在"))
		} else if err.Error() == string(apis.CertificateExistError.Code) {
			e.Error(apis.CertificateExistError.Code, err, fmt.Sprintf("修改证书校验失败，%s", "证书不存在"))
		} else {
			e.Error(500, err, fmt.Sprintf("修改证书失败，%s", err.Error()))
		}
		return
	}
	e.OK(req.GetId(), "修改成功")
}

// Delete 删除Certificate
// @Summary 删除Certificate
// @Description 删除Certificate
// @Tags Certificate
// @Param data body dto.CertificateDeleteReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "删除成功"}"
// @Router /api/v1/certificate [delete]
// @Security Bearer
func (e Certificate) Delete(c *gin.Context) {
	s := service.Certificate{}
	req := dto.CertificateDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.Remove(&req)
	if err != nil {
		if err.Error() == "已绑定域名" {
			e.Error(501, err, fmt.Sprintf("删除证书失败，%s", "已绑定域名"))
		} else {
			e.Error(500, err, fmt.Sprintf("删除证书失败，%s", err.Error()))
		}

		return
	}
	e.OK(req.GetId(), "删除成功")
}

func (e Certificate) RemoveByCertId(c *gin.Context) {
	s := service.Certificate{}
	req := dto.CertificateRemoveReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.RemoveByCertId(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("删除证书失败，%s", err.Error()))
		return
	}
	e.OK("", "删除成功")
}

func (e Certificate) Refresh(c *gin.Context) {
	s := service.Certificate{}
	req := dto.RefreshSaveReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	data, err := s.Refresh(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("刷新预热失败，%s", err.Error()))
		return
	}
	e.OK(data, "刷新预热成功")
}

func (e Certificate) RefreshWithTaskId(c *gin.Context) {
	s := service.Certificate{}
	req := dto.RefreshSaveReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	taskId, err := s.RefreshWithTaskId(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("刷新预热失败，%s", err.Error()))
		return
	}
	e.OK(taskId, "刷新预热成功")
}

func (e Certificate) GetRefreshRecord(c *gin.Context) {
	s := service.Certificate{}
	req := dto.RefreshGetReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	data, err := s.GetRefreshRecord(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取操作记录失败，%s", err.Error()))
		return
	}
	e.OK(data, "获取操作记录成功")
}

func (e Certificate) GetRefreshRecordPage(c *gin.Context) {
	s := service.Certificate{}
	req := dto.RefreshRecordGetPageReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	var count int64
	data, err := s.GetRefreshRecordPage(&req, &count)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取操作记录失败，%s", err.Error()))
		return
	}
	e.PageOK(data, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

func (e Certificate) CancelCertificateDomain(c *gin.Context) {
	s := service.Certificate{}
	req := dto.CancelCertificateDomainReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.CancelCertificateDomain(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("取消关联关系失败，%s", err.Error()))
		return
	}
	e.OK(nil, "取消关联关系成功")
}

func (e Certificate) CheckPem(c *gin.Context) {
	s := service.Certificate{}
	req := ccenginDto.CheckPemReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.CheckPem(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("证书校验失败 %s", err.Error()))
		return
	}
	e.OK(data, "证书校验成功")
}
